package jmine.tec.web.wicket.upperCase;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;

/**
 * An {@link IWrapModel} that applies upperCase
 * 
 * @author takeshi
 * @param <T> the type
 */
public class UpperCaseModelWrapper<T> implements IWrapModel<T> {

    private final IModel<T> delegate;

    /**
     * C'tor
     * 
     * @param delegate the {@link IModel}
     */
    public UpperCaseModelWrapper(IModel<T> delegate) {
        super();
        this.delegate = delegate;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.apache.wicket.model.IWrapModel#getWrappedModel()
     */
    public IModel<?> getWrappedModel() {
        return delegate;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.apache.wicket.model.IModel#getObject()
     */
    @SuppressWarnings("unchecked")
    public T getObject() {
        T t = delegate.getObject();
        if (t instanceof String) {
            return (T) t.toString().toUpperCase();
        }
        return t;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
     */
    @SuppressWarnings("unchecked")
    public void setObject(T object) {
        T value = object;
        if (object instanceof String) {
            value = (T) object.toString().toUpperCase();
        }
        delegate.setObject(value);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.apache.wicket.model.IDetachable#detach()
     */
    public void detach() {
        delegate.detach();
    }

}
